package com.sikao.app.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.CategoryAttrNewBean;
import com.sikao.app.ui.AddGoodsStepTwoActivity;
import com.sikao.app.utils.Utils;
import com.sikao.app.view.CustomExpandableListView;
import com.sikao.app.view.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 发布商品第二步 类型是3 单选 有浮动值
 */
public class MllAddStepPricesTypeThreeItemExpandableListViewAdapter extends BaseExpandableListAdapter {
    private List<CategoryAttrNewBean.ResBean.AttrBean> objects = new ArrayList<CategoryAttrNewBean.ResBean.AttrBean>();

    private Context context;
    private LayoutInflater layoutInflater;

    public   Map<Integer,View> childViewMap    ;
    public    Map<Integer,Map<Integer,View>>  parentViewMap=new HashMap<>();




    public MllAddStepPricesTypeThreeItemExpandableListViewAdapter(Context context, List<CategoryAttrNewBean.ResBean.AttrBean> objects) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.objects = objects;

    }

    @Override
    public int getGroupCount() {
        return objects.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return objects.get(i).getAttr_values().size();
    }

    @Override
    public CategoryAttrNewBean.ResBean.AttrBean getGroup(int i) {
        return objects.get(i);
    }

    @Override
    public CategoryAttrNewBean.ResBean.AttrBean.AttrValuesBean getChild(int i, int i1) {
        return objects.get(i).getAttr_values().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int parentPos, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.mll_add_step_prices_type_three_new_expan_item, null);
        }

        if (((CustomExpandableListView) viewGroup).isOnMeasure) {
            //如果是onMeasure调用的就立即返回
            return view;
        }

        childViewMap   =new HashMap<>();


        Utils.showLog("=======父组件  parentPos:" + parentPos + "  "+b);
        view.setTag(R.layout.mll_add_step_prices_type_three_new_expan_item, parentPos);
        view.setTag(R.layout.mll_add_step_prices_type_three_child_new_item, -1);
        TextView text = (TextView) view.findViewById(R.id.tv_name);
        text.setText(getGroup(parentPos).getName());
        return view;
    }

    @Override
    public View getChildView(final int parentPos, final int childPos, boolean b, View view, final ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {

            LayoutInflater inflater = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.mll_add_step_prices_type_three_child_new_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (((CustomExpandableListView) viewGroup).isOnMeasure) {
            //如果是onMeasure调用的就立即返回
            return view;
        }
         childViewMap.put(childPos,view);
        if (b){//是最后一个

             parentViewMap.put(parentPos,childViewMap);
            Utils.showLog("====已经加入父map"+ parentViewMap.size() );
            Utils.showLog("====已经加入子map"+ parentViewMap.get(parentPos).size() );

        }


       /* view.setTag(R.layout.mll_add_step_prices_type_three_new_expan_item, parentPos);
        view.setTag(R.layout.mll_add_step_prices_type_three_child_new_item, childPos);*/

        //处理子项数据


        //view.setTag(childPos,cbThreeChildIsDefault);

        CategoryAttrNewBean.ResBean.AttrBean.AttrValuesBean object = getChild(parentPos, childPos);

        for (int i = 0; i < AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getDefault_value().size(); i++) {
            if (object.getId() == AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getDefault_value().get(i).getId()) {
                Utils.showLog("======= 使用值被选中  parentPos:" + parentPos + "  childPos:" + childPos + "  object.getId() :" + object.getId());

                viewHolder.cbThreeChildIsUse.setChecked(true);

                viewHolder.cbThreeChildIsDefault.setEnabled(true);
                viewHolder.cbThreeChildIsDefault.setClickable(true);
                //判断是不是默认值
                if (AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getDefault_value().get(i).isDefault()) {
                    viewHolder.cbThreeChildIsDefault.setChecked(true);
                    Utils.showLog("======= 默认值被选中  parentPos:" + parentPos + "  childPos:" + childPos);
                }
                if ( AddGoodsStepTwoActivity.addTo!=1) {//1是拍卖
                    viewHolder.etThreeChildChangePrice.setEnabled(true);
                    viewHolder.etThreeChildChangePrice.setClickable(true);
                }

                //修改单选数据组 属性值
                AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(childPos).setChecked(true);

                //给默认的浮动值复制
                viewHolder.etThreeChildChangePrice.setText(AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getDefault_value().get(i).getChangePrice());
                //将默认值中的值附加到要提交的数据上
                AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(childPos).setDefault(AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getDefault_value().get(i).isDefault());
                AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(childPos).setChangePrice(AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getDefault_value().get(i).getChangePrice());


            }
        }


        /*添加事件***************************************************************************************/
        viewHolder.tvThreeChildValue.setText(object.getValue());
        if (AddGoodsStepTwoActivity.canEdit) {
            viewHolder.cbThreeChildIsUse.setEnabled(true);
            viewHolder.cbThreeChildIsUse.setClickable(true);
          /*  etThreeChildChangePrice.setEnabled(true);
            etThreeChildChangePrice.setClickable(true);*/
        } else {
            viewHolder.cbThreeChildIsUse.setEnabled(false);
            viewHolder.cbThreeChildIsUse.setClickable(false);
            viewHolder.etThreeChildChangePrice.setEnabled(false);
            viewHolder.etThreeChildChangePrice.setClickable(false);
            //默认是不可以选择状态
            viewHolder.cbThreeChildIsDefault.setEnabled(false);
            viewHolder.cbThreeChildIsDefault.setClickable(false);
        }
        final ViewHolder finalViewHolder = viewHolder;
        viewHolder.cbThreeChildIsUse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    if ( AddGoodsStepTwoActivity.addTo!=1) {//1是拍卖
                        finalViewHolder.etThreeChildChangePrice.setEnabled(true);
                        finalViewHolder.etThreeChildChangePrice.setClickable(true);
                    }
                    AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(childPos).setChecked(true);
                    //默认是可以选择状态
                    finalViewHolder.cbThreeChildIsDefault.setEnabled(true);
                    finalViewHolder.cbThreeChildIsDefault.setClickable(true);
                } else {
                    finalViewHolder.etThreeChildChangePrice.setEnabled(false);
                    finalViewHolder.etThreeChildChangePrice.setClickable(false);
                    AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(childPos).setChecked(false);
                    //默认是不可以选择状态
                    finalViewHolder.cbThreeChildIsDefault.setEnabled(false);
                    finalViewHolder.cbThreeChildIsDefault.setClickable(false);
                    //同时取消选中默认状态
                    finalViewHolder.cbThreeChildIsDefault.setChecked(false);
                    AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(childPos).setDefault(false);
                }
            }
        });


        viewHolder.cbThreeChildIsDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(childPos).setDefault(true);
                    //保证默认值只有一个


                    for (int i = 0; i < AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().size(); i++) {
                        if (AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(i).isDefault() && i != childPos) {
                            //将是否默认值设置为0 取消选中状态
                            AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(i).setDefault(false);
                            //此处要修改
                            //  (ViewGroup) ExpandableListView.getPackedPositionForGroup(parentPos)

                            Utils.showLog("====i:" + i + " 也被选中 父类index:"+ parentPos+" 子类index："+childPos );

                         ((ViewHolder)parentViewMap.get(parentPos).get(i).getTag()) .cbThreeChildIsDefault.setChecked(false);

                            Utils.showLog("====i:" + i + " 取消的是"+  ((ViewHolder)parentViewMap.get(parentPos).get(i).getTag()).tvThreeChildValue.getText().toString());
                            //( (ViewHolder) ((ViewGroup)v.getParent()).getChildAt(i).getTag()).cbThreeChildIsDefault.setChecked(false);
                            //( (ViewHolder) v.getParent().getChildAt(i).getTag()).cbThreeChildIsDefault.setChecked(false);
                            //( (ViewHolder)  ((CustomExpandableListView)viewGroup.getChildAt(parentPos)).getChildAt(i).getTag()).cbThreeChildIsDefault.setChecked(false);
                        }

                    }

                } else {
                    AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(childPos).setDefault(false);
                }
            }
        });

        viewHolder.etThreeChildChangePrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(final Editable editable) {

                TimerTask task = new TimerTask() {
                    public void run() {
                        //execute the task
                        AddGoodsStepTwoActivity.resTypeThree.get(parentPos).getAttr_values().get(childPos).setChangePrice(editable.toString());
                    }
                };
                Timer timer = new Timer();
                timer.schedule(task, 1500);
            }
        });


        return view;
    }

    protected class ViewHolder {
        private CheckBox cbThreeChildIsUse;
        private CheckBox cbThreeChildIsDefault;
        private TextView tvThreeChildValue;
        private EditText etThreeChildChangePrice;

        public ViewHolder(View view) {
            cbThreeChildIsDefault = (CheckBox) view.findViewById(R.id.cb_three_child_isdefault);
            cbThreeChildIsUse = (CheckBox) view.findViewById(R.id.cb_three_child_isuse);

            tvThreeChildValue = (TextView) view.findViewById(R.id.tv_three_child_value);
            etThreeChildChangePrice = (EditText) view.findViewById(R.id.et_three_child_change_price);
        }
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}
